import {verdictDate} from "@/core/utils/validate";

// 获取时间戳 dateStr格式为yyyy-MM 或yyyy-MM-dd
export const getDate = (dateStr: string) => {
    if (!verdictDate(dateStr)) {
        dateStr += "-01";
    }
    let date: Date = new Date(dateStr);
    return date;
};
// 拆分月份区间的所有月份
export const breakUpMonth = (startDate: string, endDate: string) => {
    if(!startDate || !endDate){
        //////console.log("开始或结束时间为空");
        return [];
    }
    let startTime = getDate(startDate);
    let endTime = getDate(endDate);
    let dateArr = [];
    while ((endTime.getTime() - startTime.getTime()) >= 0) {
        let year = startTime.getFullYear();
        let month = (startTime.getMonth() + 1).toString().length == 1 ? "0" + (startTime.getMonth() + 1).toString() : (startTime.getMonth() + 1);
        dateArr.push(year + "-" + month);
        startTime.setMonth(startTime.getMonth() + 1);
    }
    return dateArr;
};

// 拆分年份区间的所有年份
export const breakUpYear = (startYear: string, endYear: string) => {
    if(!startYear || !endYear){
        //////console.log("开始或结束时间为空");
        return [];
    }
    let startTime = new Date(startYear + "-01-01");
    let endTime = new Date(endYear + "-12-31");
    let dateArr = [];
    while ((endTime.getTime() - startTime.getTime()) >= 0) {
        let year = startTime.getFullYear();
        dateArr.push(year);
        startTime.setFullYear(startTime.getFullYear() + 1);
    }
    return dateArr;
};
